$(document).ready(function () {
    var accId = sessionStorage.phone;
    var colpageNum = 1;
    var colpagesize = 8;
    var coltol = 1;
    var colldata = {
        accId: accId,
        pageNum: colpageNum,
        pageSize: colpagesize
    };
    var metol=1;
    var merchantRemark;
    var merchantId;

    $("#list-zhanhui").click(function () {
        $("#clncard").css("display", "none");
        $("#clncont").css("display", "block");
        $("#list-zhanhui").attr("class", "btn-exh-list-ex");
        $("#list-zhanshang").attr("class", "btn-exh-list-car");

    });

    $("#list-zhanshang").click(function () {
        $("#clncard").css("display", "block");
        $("#clncont").css("display", "none");
        $("#list-zhanhui").attr("class", "btn-exh-list-car");
        $("#list-zhanshang").attr("class", "btn-exh-list-ex");
          var pageNum=1;
          var pagesize=8;
        var dat={
            accId: accId,
            pageNum:pageNum ,
            pageSize: pagesize
        };
         merfun(dat);
        $.jqPaginator('#pagination', {
            totalPages: metol,
            visiblePages: 10,
            currentPage: 1,
            wrapper: '<ul class="pagination" style="margin: 0 0"></ul>',
            first: '<li class="first"><a href="javascript:void(0);">首页</a></li>',
            prev: '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
            next: '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
            last: '<li class="last"><a href="javascript:void(0);">最后一页</a></li>',
            page: '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
            onPageChange: function (num) {
                if (num > 0) {
                    pageNum=num;
                    dat={
                        accId: accId,
                        pageNum:pageNum ,
                        pageSize: pagesize
                    };
                    merfun(dat);
                }
            }
        });
    });

     //获取用户所有展商收藏的方法
     function merfun(medata) {
         $("#plans").empty();         //清除原来的内容 id=plans标签的内容
         $.ajax({
             type: "POST",
             url: "http://www.enet720.net:8080/zwkj/merchant/findBytranslate",
             contentType: 'application/json;charset=UTF-8',
             dataType: 'json',
             async: false,
             data: JSON.stringify(medata),
             success: function (data) {
                 var d = data.data;
                 if(d.total> 0){
                     // 如果数据查询成功，但是数据为空的判断处理
                         metol = Math.ceil(d.total / 8);
                         if (metol < 1) {
                             metol = 1;
                         }
                     var list = d.list;
                     for (var i = 0; i < list.length; i++) {
                         $("#plans").append("        <ul class=\"merchant-to-out\">\n" +
                             "                <li>\n" +
                             "                    <div class=\"row\">\n" +
                             "                        <div class=\"col-xs-2 col-sm-2 col-md-2 col-lg-1\">\n" +
                             "                            <span class=\"elli\" name='merid' title='" + list[i].merchantId + "'>" + list[i].merchantId + "</span>\n" +
                             "                        </div>\n" +
                             "                        <div class=\"col-xs-2 col-sm-2 col-md-2 col-lg-3\">\n" +
                             "                            <span class=\"elli\" title='" + list[i].merchantName + "'>" + list[i].merchantName + "</span>\n" +
                             "                        </div>\n" +
                             "                        <div class=\"col-xs-2 col-sm-2 col-md-2 col-lg-2\">\n" +
                             "                            <span class=\"elli\" title='" + list[i].merchantExposition + "'>" + list[i].merchantExposition + "</span>\n" +
                             "                        </div>\n" +
                             "                        <div class=\"col-xs-2 col-sm-2 col-md-2 col-lg-2\">\n" +
                             "                            <span class=\"elli\" title='" + list[i].merchantEMail + "'>" + list[i].merchantEMail + "</span>\n" +
                             "                        </div>\n" +
                             "                        <div class=\"col-xs-2 col-sm-2 col-md-2 col-lg-2\">\n" +
                             "                            <span class=\"elli\" title='" + list[i].merchantWebsite + "'>" + list[i].merchantWebsite + "</span>\n" +
                             "                        </div>\n" +
                             "                        <div class=\"col-xs-2 col-sm-2 col-md-2 col-lg-2\">\n" +
                             "                            <span class=\"elli\" title='" + list[i].merchantTelephone + "'>" + list[i].merchantTelephone + "</span>\n" +
                             "                        </div>\n" +
                             "                    </div>\n" +
                             "                </li>\n" +
                             "            </ul>");
                     }
                 } else {
                     $("#plans").append("抱歉，你并没有收藏展商！........请查看其余展会，收藏展会的内容哦！");
                     metol = 1;
                 }

             },
             error: function () {
                 alert("出错了,抱歉！")
             }
         });
         //点击展商展示展商详情信息
         $("#plans").find("li").click(function () {
              merchantId = $(this).find($("[name=merid]")).text();
             var adata = {
                 accId:accId,
                 mctId: merchantId
             };
             $.ajax({
                 type: "POST",
                 url: "http://www.enet720.net:8080/zwkj/merchant/findBytranslateId",
                 contentType: 'application/json;charset=UTF-8',
                 dataType: 'json',
                 async: false,
                 data: JSON.stringify(adata),
                 success: function (data) {
                     var d = data.data;
                     // 如果数据查询成功，但是数据为空的判断处理
                     if (!(d == null)) {
                         var merchantRemark = d.collectMerchants;
                         merchantId = d.merchantId;              //展商id
                         var merchantImgAddress = d.merchantImgAddress;      //图片地址
                         var merchantNature = d.merchantNature;                  //是否特装  0表示标摊 1表示特装
                         var merchantPerson = d.merchantPerson;              //联系人
                         var merchantPhone = d.merchantPhone;                //联系电话
                         var merchantTelephone = d.merchantTelephone;           //公司电话
                         var merchantName = d.merchantName;                     //展商名称
                         var merchantExposition = d.merchantExposition;          //展位号
                         var merchantEMail = d.merchantEMail;                    //展商邮箱
                         var merchantFax = d.merchantFax;                        //展商传真
                         var merchantWebsite = d.merchantWebsite;                //展商网址
                         var merchantAddress = d.merchantAddress;                 //展商地址
                         var Remark =merchantRemark.remark;                     //展商备注
                         var zanl = "正在努力寻找....";

                         merchantImgAddress = "../resource/img/zhantia2.jpg";
                         if (merchantName === "") {
                             merchantName = zanl;
                         }
                         if (merchantNature === "0") {
                             merchantNature = "标摊"
                         } else {
                             merchantNature = "特装"
                         }
                         if (merchantExposition === "") {
                             merchantExposition = zanl;
                         }
                         if (merchantEMail === "") {
                             merchantEMail = zanl;
                         }
                         if (merchantFax === "") {
                             merchantFax = zanl;
                         }
                         if (merchantWebsite === "") {
                             merchantWebsite = zanl;
                         }
                         if (merchantTelephone === "") {
                             merchantTelephone = zanl;
                         }
                         if (merchantPerson === "") {
                             merchantPerson = zanl;
                         }
                         if (merchantPhone === "") {
                             merchantPhone = zanl;
                         }
                         if (merchantAddress === "") {
                             merchantAddress = zanl;
                         }

                         $("#merchantImgAddress").css("background-image", "url(" + merchantImgAddress + ")");
                         $("#property").val(merchantNature);
                         $("#property").attr("title",merchantNature);
                         $("#merchantName").val(merchantName);
                         $("#merchantName").attr("title",merchantName);
                         $("#merchantExposition").val(merchantExposition);
                         $("#merchantExposition").attr("title",merchantExposition);
                         $("#merchantEMail").val(merchantEMail);
                         $("#merchantEMail").attr("title",merchantEMail);
                         $("#merchantFax").val(merchantFax);
                         $("#merchantFax").attr("title",merchantFax);
                         $("#merchantWebsite").val(merchantWebsite);
                         $("#merchantWebsite").attr("title",merchantWebsite);
                         $("#merchantTelephone").val(merchantTelephone);
                         $("#merchantTelephone").attr("title",merchantTelephone);
                         $("#merchantPerson").val(merchantPerson);
                         $("#merchantPerson").attr("title",merchantPerson);
                         $("#merchantPhone").val(merchantPhone);
                         $("#merchantPhone").attr("title",merchantPhone);
                         $("#merchantAddress").val(merchantAddress);
                         $("#merchantAddress").attr("title",merchantAddress);
                         $("#merchantRemark").val(Remark);
                         $("#merchantRemark").attr("title",Remark);
                     } else {

                     }
                 },
                 error: function () {
                     alert("出错了,抱歉！")
                 }
             });
         })
     }
     window.load=collfun(colldata);
     //获取所有展会收藏的方法
     function  collfun(data) {
         $("#bapi").empty();//清空以前的收藏的展会
         $.ajax({
             url: "http://www.enet720.net:8080/zwkj/exhibition/findBytransalte",
             type: "POST",
             dataType: "json",
             data: JSON.stringify(data),
             async: false,//异步请求
             contentType: "application/json;charset=utf-8",
             success: function (data1) {
                 if (data1.code === 0) {
                     var coldata=data1.data.list;
                     coltol=Math.ceil(data1.data.total / 8);
                     if(coltol===0){
                         coltol=1;
                     }
                     if(!(coldata.length===0)){
                         for (var j = 0; j < coldata.length; j++) {
                             $("#bapi").append( "<div class='col-xs-12 col-sm-6 col-md-4 col-lg-3' name='"+coldata[j].exhibitionId+"'>\n" +
                                 "                <div class='exhibition_list'>\n" +
                                 "                    <div class='list-title'>\n" +
                                 "                        <span>" + coldata[j].exhibitionName + "</span>\n" +
                                 "                    </div>\n" +
                                 "                    <div class='list-content'>\n" +
                                 "                        <span class='list-content-title'>\n" +
                                 "                        ---" + coldata[j].exhibitionAddress + "\n" +
                                 "                        </span>\n" +
                                 "                    </div>\n" +
                                 "                    <div class='exhibition-style'>行业分类 - " + coldata[j].exhibitionStyle + "</div>\n"+
                                 "   j                 <div class='div-Radius'></div>\n" +
                                 "                    <span class='list-span'>" + coldata[j].exhibitionPrice + "</span>\n" +
                                 "                    <span class='list-time-a'></span>\n" +
                                 "                    <span class='list-time-b' title='" + coldata[j].exhibitionStart + "---" + coldata[j].exhibitionEnd + "'>" + coldata[j].exhibitionStart + "---" + coldata[j].exhibitionEnd + "</span>\n" +
                                 "                    <a href='../HTML/merchant-exh.html?exhibitionId=" + coldata[j].exhibitionId + "&exhibitionName=" + coldata[j].exhibitionName + "' class='list-number'>" + coldata[j].exhibitionMerchantCount + "位展商</a>\n" +
                                 "                </div>\n" +
                                 "            </div>");

                         }
                     }else {
                         $("#bapi").append("您还没有收藏展会信息信息！");
                         window.location="../HTML/plan-exhibition.html";
                     }

                 }else {
                     $("#bapi").append("请求失败");
                 }
             },
             error: function () {
                 alert("请求超时请重试！");
             }
         });
     }
    $.jqPaginator('#pagination', {
        totalPages: coltol,
        visiblePages: 10,
        currentPage: 1,
        wrapper: '<ul class="pagination" style="margin: 0 0"></ul>',
        first: '<li class="first"><a href="javascript:void(0);">首页</a></li>',
        prev: '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
        next: '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
        last: '<li class="last"><a href="javascript:void(0);">最后一页</a></li>',
        page: '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
        onPageChange: function (num) {
            if (num > 0) {
                colpageNum=num;
                colldata = {
                    accId: accId,
                    pageNum: colpageNum,
                    pageSize: colpagesize
                };
                collfun(colldata);
            }
        }
    });
    // 点击保存，文本框可以操作 修改展商备注
    $("#btncollect").click(function () {
        merchantRemark=$("#merchantRemark").val();
            var str = {
                accId: accId,              //用户id
                mctId: merchantId,                  //展商id
                remark: merchantRemark
            };

            if (!(merchantId === undefined && accId === undefined)) {
                $.ajax({
                    type: "POST",
                    url: "http://www.enet720.net:8080/zwkj/merchant/updateByid",
                    contentType: 'application/json;charset=UTF-8',
                    dataType: 'json',
                    async: false,
                    data: JSON.stringify(str),
                    success: function (data) {
                        var data = data.data;
                        if (!data) {
                            console.log("保存失败")
                        } else {
                            alert("保存成功")
                        }
                    },
                    error: function () {
                        alert("出错了,抱歉！")
                    }
                });
            } else {
                alert("保存失败！没有用户名或者没有选中展商所以失败了。")
            }

    });
    var datajson1={};//添加订单的参数
    //点击单个展会价格购买
    $('body').on("click",".list-span",function(){
        datajson1={
            "accId":sessionStorage.phone,
            "mallId":$(this).parent().parent().attr("name")
        };
        var id=new Date().getTime();//订单id
        //添加订单
        $.ajax({
            type: "POST",
            url: "http://www.enet720.net:8080/zwkj/purchase/orders/exhibition/"+id+"",
            contentType: 'application/json;charset=UTF-8',
            dataType: 'json',
            async: false,
            data: JSON.stringify(datajson1),
            success: function (data) {
                if(data.code=="0"){
                    //查询订单
                    $.ajax({
                        type: "GET",
                        url: "http://www.enet720.net:8080/zwkj/purchase/orders/"+id+"",
                        contentType: 'application/json;charset=UTF-8',
                        dataType: 'json',
                        async: false,
                        success: function (data) {
                            if(data.code=="0"){
                                $("#iphone",parent.document).text(sessionStorage.phone);
                                $("#login_name",parent.document).text(sessionStorage.login_name);
                                $("#mall-id",parent.document).text(data.data.mallId);
                                $("#mall-name",parent.document).text(data.data.name);
                                $("#id",parent.document).text(data.data.id);
                                $("#mall-desc",parent.document).text(data.data.mallDesc);
                                $("#price",parent.document).text(data.data.price);
                                $("#order",parent.document).css("display","block");
                                $(".shade",parent.document).css("display","block");
                            }else{
                                alert("数据异常请重试！");
                                location.reload();
                            }
                        },
                        error: function () {
                            alert("请求超时请重试!");
                            location.reload();
                        }
                    });
                }
                if(data.code==null){
                    alert("展会不存在");
                    location.reload();
                }
            },
            error: function () {
                alert("出错了，抱歉！");
                location.reload();
            }
        });
    });
    //点击刷新重新加载
    $("#btncollect1").click(function () {
        location.reload();
    });
    //禁用文本框
    $("body").find("[name='dis']").attr("disabled", "disabled");
});